package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.RoleStatusDto;
import com.example.entity.Role;
import com.example.entity.RoleMenu;
import com.example.entity.Tag;
import com.example.service.MenuService;
import com.example.service.RoleMenuService;
import com.example.service.RoleService;
import com.example.mapper.RoleMapper;
import com.example.utils.BeanCopyUtils;
import com.example.utils.ResponseResult;
import com.example.utils.SecurityUtils;
import com.example.vo.MenuCheckedVo;
import com.example.vo.MenuVo;
import com.example.vo.PageVo;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author sprite-pc
* @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
* @createDate 2023-12-29 12:57:18
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(id == 1L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult getList(int pageNum, int pageSize, String roleName, String status) {
        Page<Role> rolePage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (roleName!=null){
            queryWrapper.like(Role::getRoleName,roleName);
        }
        if(!StringUtils.isNullOrEmpty(status)){
            queryWrapper.eq(Role::getStatus,status);
        }
        queryWrapper.orderByAsc(Role::getRoleSort);
        page(rolePage,queryWrapper);
        PageVo pageVo = new PageVo(rolePage.getRecords(),rolePage.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(RoleStatusDto roleStatusDto) {
        Role role = roleService.getById(roleStatusDto.getRoleId());
        role.setStatus(roleStatusDto.getStatus());
        updateById(role);
        return ResponseResult.okResult();
    }


}




